﻿using System;
using System.Web;
using System.Xml.Serialization;

using SS4U.Library.ConfigurationClasses;

namespace SS4U.Library
{
// ReSharper disable ClassNeverInstantiated.Global
    public class Configuration
// ReSharper restore ClassNeverInstantiated.Global
    {
        private const string Configfilename = "SimpleSearch4Umbraco.config";
        private const string Cachekey = "SimpleSearchConfiguration";

        public static SearchDefinition DefaultSearchDefinition
        {
            get
            {
                var root = (ConfigurationRoot)HttpRuntime.Cache[Cachekey] ?? LoadConfiguration();
                return root.SearchDefinitions.GetDefault();
            }
        }

        public static ConfigurationRoot Root
        {
            get
            {
                return (ConfigurationRoot)HttpRuntime.Cache[Cachekey] ?? LoadConfiguration();
            }
        }

        public static SearchDefinition GetSearchDefinition(string searchDefinitionName)
        {
            var root = (ConfigurationRoot)HttpRuntime.Cache[Cachekey] ?? LoadConfiguration();
            return root.SearchDefinitions.GetByName(searchDefinitionName);
        }

// ReSharper disable UnusedMember.Global
        public static void ClearConfiguration()
// ReSharper restore UnusedMember.Global
        {
            HttpRuntime.Cache.Remove(Cachekey);
        }

        public static void ReloadConfiguration()
        {
            HttpRuntime.Cache.Remove(Cachekey);
            HttpRuntime.Cache[Cachekey] = LoadConfiguration();
        }

        private static ConfigurationRoot LoadConfiguration()
        {
            umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, 0, "Loading SS4U Configuration");

            var ser = new XmlSerializer(typeof(ConfigurationRoot));
            var path = umbraco.GlobalSettings.FullpathToRoot + System.IO.Path.DirectorySeparatorChar + "config" + System.IO.Path.DirectorySeparatorChar + Configfilename;

            using (var fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
            {
                try
                {
                    var retVal = (ConfigurationRoot)ser.Deserialize(fs);
                    fs.Close();
                    HttpRuntime.Cache.Insert(Cachekey, retVal, new System.Web.Caching.CacheDependency(path));
                    return retVal;
                }
                catch (Exception ex)
                {
                    umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error, 0, "Error loading SS4U Configuration: " + ex.Message + ex.StackTrace);
                    return null;
                }
            }
        }

// ReSharper disable UnusedMember.Local
        public static void SaveConfiguration()
// ReSharper restore UnusedMember.Local
        {
            var ser = new XmlSerializer(typeof(ConfigurationRoot));
            var path = umbraco.GlobalSettings.FullpathToRoot + System.IO.Path.DirectorySeparatorChar + "config" + System.IO.Path.DirectorySeparatorChar + Configfilename;

            using (var fs = new System.IO.FileStream(path, System.IO.FileMode.Create))
            {
                ser.Serialize(fs, Root);
                fs.Close();
            }
            HttpRuntime.Cache[Cachekey] = Root;
        }

        public static void CreateSampleConfiguration()
        {
            var testRoot = new ConfigurationRoot();
            var sd = new SearchDefinition();
            testRoot.SearchDefinitions.Add(sd);

            var dt = new DocumentType { Alias = "dtMitarbeiter", Category = "Mitarbeiter" };
            dt.Properties.Add(new Property { Alias = "Vorname", Comparison = Comparison.Contains, ComparisonType = ComparisonType.String });
            sd.DocumentTypes.Add(dt);

            var ser = new XmlSerializer(typeof(ConfigurationRoot));
            var path = umbraco.GlobalSettings.FullpathToRoot + System.IO.Path.DirectorySeparatorChar + "config" + System.IO.Path.DirectorySeparatorChar + "test_" + Configfilename;

            using (var fs = new System.IO.FileStream(path, System.IO.FileMode.Create))
            {
                ser.Serialize(fs, testRoot);
                fs.Close();
            }
        }

    }
}
